package com.phospherus.flow.service

import com.phospherus.flow.entity.TcflefttofactoryLeftapply
import com.phospherus.flow.entity.TcflefttofactoryQualityassurance
import com.phospherus.flow.entity.TqTable1
import com.phospherus.flow.repository.TcflefttofactoryLeftapplyRespository
import com.phospherus.flow.repository.TcflefttofactoryQualityassuranceRespository
import com.phospherus.flow.repository.TqTable1Respository
import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * 流程4
 *
 * @author songjinlong
 * @version 1.0
 * @since 2018/1/3
 */
@Service
class TcfLeftToFactoryService : FormInstBaseService() {
    @Autowired
    lateinit var tcflefttofactoryLeftapplyRespository: TcflefttofactoryLeftapplyRespository
    @Autowired
    lateinit var tcflefttofactoryQualityassuranceRespository: TcflefttofactoryQualityassuranceRespository
    @Autowired
    lateinit var tqTable1Respository: TqTable1Respository
    override fun getFormInstServiceName(): String = "tcflefttofactory"

    override fun save(map: MutableMap<String, Any>) {
        var formInstId = map["formInstId"] as Long

        var tcflefttofactoryLeftapplyMap = map["tcflefttofactory_leftapply"] as Map<String, Any>?
        if (tcflefttofactoryLeftapplyMap != null) {
            var tcflefttofactoryLeftapply = createEntityByMap<TcflefttofactoryLeftapply>(tcflefttofactoryLeftapplyMap, TcflefttofactoryLeftapply::class.java)
            tcflefttofactoryLeftapply.formInstId = formInstId
            tcflefttofactoryLeftapply = tcflefttofactoryLeftapplyRespository.save(tcflefttofactoryLeftapply)
            if (StringUtils.isNotBlank(tcflefttofactoryLeftapplyMap["attachedFiles"] as? String)) {
                attachedFileService.addFormInstAttachedFile(tcflefttofactoryLeftapplyMap["attachedFiles"].toString(), formInstId, "tcflefttofactory_leftapply", tcflefttofactoryLeftapply.id)
            }
        }

        var tcflefttofactoryQualityassuranceMap = map["tcflefttofactory_qualityassurance"] as Map<String, Any>?
        if (tcflefttofactoryQualityassuranceMap != null) {
            var tcflefttofactoryQualityassurance = createEntityByMap<TcflefttofactoryQualityassurance>(tcflefttofactoryQualityassuranceMap, TcflefttofactoryQualityassurance::class.java)
            tcflefttofactoryQualityassurance.formInstId = formInstId
            tcflefttofactoryQualityassurance = tcflefttofactoryQualityassuranceRespository.save(tcflefttofactoryQualityassurance)
            if (StringUtils.isNotBlank(tcflefttofactoryQualityassuranceMap["attachedFiles"] as? String)) {
                attachedFileService.addFormInstAttachedFile(tcflefttofactoryQualityassuranceMap["attachedFiles"].toString(), formInstId, "tcflefttofactory_qualityassurance", tcflefttofactoryQualityassurance.id)
            }
            var tqTable1List = tcflefttofactoryQualityassuranceMap["tq_table1"] as List<Map<String, Any>>?
            var tqTable1s: List<TqTable1>? = null
            if (tqTable1List != null) {
                tqTable1s = tqTable1List.mapTo(ArrayList(), {
                    with(createEntityByMap<TqTable1>(it, TqTable1::class.java)) {
                        this.id = null
                        this.formInstId = formInstId
                        this.tcffqId = tcflefttofactoryQualityassurance.id
                        this
                    }
                })
            }
            var tables = tqTable1Respository.findByFormInstIdAndTcffqId(formInstId, tcflefttofactoryQualityassurance.id!!)
            tqTable1Respository.delete(tables)
            if(tqTable1s != null){
                tqTable1Respository.save(tqTable1s)
            }
        }
    }

    override fun query(formInstId: Long): MutableMap<String, Any?>? {
        var map = mutableMapOf<String, Any?>()
        var tcflefttofactoryLeftapply = tcflefttofactoryLeftapplyRespository.findByFormInstId(formInstId)
        tcflefttofactoryLeftapply.attachedFileList = attachedFileService.getFormAttachedFiles(formInstId, tcflefttofactoryLeftapply.id, "tcflefttofactory_leftapply")
        map["tcflefttofactory_leftapply"] = tcflefttofactoryLeftapply

        var tcflefttofactoryQualityassurance = tcflefttofactoryQualityassuranceRespository.findByFormInstId(formInstId)
        tcflefttofactoryQualityassurance.tq_table1 = tqTable1Respository.findByFormInstIdAndTcffqId(formInstId, tcflefttofactoryQualityassurance.id!!)
        tcflefttofactoryQualityassurance.attachedFileList = attachedFileService.getFormAttachedFiles(formInstId, tcflefttofactoryQualityassurance.id, "tcflefttofactory_qualityassurance")
        map["tcflefttofactory_qualityassurance"] = tcflefttofactoryQualityassurance

        return map
    }
}